package nc.ui.uif2.actions;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.event.EventListenerList;
import nc.ui.pub.ButtonObject;
import nc.ui.pub.ToftPanel;
import nc.ui.uif2.DefaultExceptionHanler;
import nc.ui.uif2.IExceptionHandler;
import nc.ui.uif2.NCAction;

public class DefaultToftPanelActionContainer
  implements IToftPanelActionContainer
{
  private static final long serialVersionUID = -6595600984249195578L;
  private ToftPanel toftPanel;
  private JComponent containerUI;
  private boolean contributeorActived = true;
  private IExceptionHandler exceptionHandler;
  private List<NCAction> actions = new ArrayList();

  private EventListenerList listeners = new EventListenerList();

  public DefaultToftPanelActionContainer(IToftPanelActionContainer containerUI)
  {
    this.containerUI = ((JComponent)containerUI);
    initExceptionHandler();
  }

  private void initExceptionHandler() {
    this.exceptionHandler = new DefaultExceptionHanler()
    {
      public JComponent getContainerUI()
      {
        return DefaultToftPanelActionContainer.this.containerUI;
      }
    };
  }

  public void addActionsChangedListener(IActionsChangedListener listener)
  {
    this.listeners.add(IActionsChangedListener.class, listener);
  }

  public void setToftPanel(ToftPanel toftPanel) {
    this.toftPanel = toftPanel;
    for (NCAction action : this.actions)
      action.setToftPanel(toftPanel);
  }

  public void updateButtonObjects()
  {
    for (NCAction action : this.actions)
      action.updateStatus();
  }

  public JComponent getContainerUI()
  {
    return this.containerUI;
  }

  public void addAction(NCAction action) {
    ArrayList newActions = new ArrayList();
    newActions.addAll(getActions());
    newActions.add(action);
    setActions(newActions);
  }

  public void removeAction(NCAction action) {
    ArrayList newActions = new ArrayList();
    newActions.addAll(getActions());
    newActions.remove(action);
    setActions(newActions);
  }

  public List<NCAction> getActions() {
    return this.actions;
  }

  public void setActions(List<NCAction> actions) {
    this.actions = actions;
    if (this.toftPanel != null)
    {
      setToftPanel(this.toftPanel);
    }
    fireActionsChangedEvent();
  }

  private void fireActionsChangedEvent()
  {
    IActionsChangedListener[] ls = (IActionsChangedListener[])this.listeners.getListeners(IActionsChangedListener.class);
    for (IActionsChangedListener listener : ls)
      listener.actionsChanged();
  }

  public void onButtonClicked(ButtonObject bo)
  {
    for (NCAction action : this.actions)
    {
      if (action.match(bo))
      {
        try
        {
          action.actionPerformed(new ActionEvent(bo, 0, null));
        }
        catch (RuntimeException e) {
          this.exceptionHandler.handlerExeption(e);
        }
      }
    }
  }

  public ButtonObject[] getButtons()
  {
    ArrayList btns = new ArrayList();
    for (NCAction action : this.actions) {
      btns.add(action.getButtonObject());
    }
    return (ButtonObject[])btns.toArray(new ButtonObject[0]);
  }

  public void addActionContributorListener(IActionContributorListener l) {
    this.listeners.remove(IActionContributorListener.class, l);
    this.listeners.add(IActionContributorListener.class, l);
  }

  public boolean isActived()
  {
    return this.contributeorActived;
  }

  public void removeActionContributorListener(IActionContributorListener l) {
    this.listeners.remove(IActionContributorListener.class, l);
  }

  public void setActived(boolean actived)
  {
    if (this.contributeorActived == actived)
      return;
    this.contributeorActived = actived;

    IActionContributorListener[] ls = (IActionContributorListener[])this.listeners.getListeners(IActionContributorListener.class);
    for (IActionContributorListener contributeorListener : ls)
      contributeorListener.contributorStateChanged(new ActionContributorEvent((IToftPanelActionContainer)this.containerUI));
  }
}